package pool.impl;

import pool.impl.sql.ConnectionPool;

import java.sql.Connection;
import java.util.concurrent.TimeUnit;

/**
 * Created mangcao with 2019/5/30 14:57
 * Description:
 */
public class ConnectionPoolMain {

   static ConnectionPool pool = new ConnectionPool(1);

    static Integer SLEEP_TIME = 1;


    public static void main(String[] args) {

        try {
            Connection connection = pool.fetchConnection(0);
            System.out.println("1");
            TimeUnit.MILLISECONDS.sleep(SLEEP_TIME);

            // 模拟一个用户线程释放连接
            new Thread(() -> {
                System.out.println("开始睡觉");
                try {
                    Thread.sleep(SLEEP_TIME);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("睡醒了");

                pool.releaseConnection(connection);
                System.out.println(Thread.currentThread().getName());

            }).start();

            Connection connection1 = pool.fetchConnection(SLEEP_TIME * 7);
            if (connection1 == null) {
                System.out.println("炸裂了，没有数据");
            }
            System.out.println("2");


        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
